/* Directives */
var appDict = angular.module("iDirective", []);

//css动态加载
appDict.directive('head', ['$rootScope','$compile',
    function($rootScope, $compile){
        return {
            restrict: 'E',
            link: function(scope, elem){
                var html = '<link rel="stylesheet" ng-repeat="(routeCtrl, cssUrl) in routeStyles" ng-href="{{cssUrl}}" >';
                elem.append($compile(html)(scope));
                scope.routeStyles = {};
                $rootScope.$on('$routeChangeStart', function (e, next, current) {
                    if(current && current.$$route && current.$$route.css){
                        if(!Array.isArray(current.$$route.css)){
                            current.$$route.css = [current.$$route.css];
                        }
                        angular.forEach(current.$$route.css, function(sheet){
                            scope.routeStyles[sheet] = undefined;
                        });
                    }
                    if(next && next.$$route && next.$$route.css){
                        if(!Array.isArray(next.$$route.css)){
                            next.$$route.css = [next.$$route.css];
                        }
                        angular.forEach(next.$$route.css, function(sheet){
                            scope.routeStyles[sheet] = sheet;
                        });
                    }
                });

            }
        };
    }
]);

// 侧边栏
appDict.directive('menuCont',[function(){
    return{
        restrict:'EA',
        link:function(scope, ele){
            var box = ele.find('.menu-cont');
            if($(window).width() > $config.sreen){
                // pc端
                /*$('.menu-side').click(function () {

                })*/
                scope.showCont = function () {
                    box.animate({right:'0px'},400);
                }
                $('.menu-close').click(function () {
                    box.animate({right:'-700px'},400)
                })
                ele.find('.tit li').mouseenter(function () {
                    var index = $(this).index();
                    $(this).addClass('hover').siblings().removeClass('hover');
                    $(ele.find('.cont').eq(index)).fadeIn(400).siblings('.cont').fadeOut(200);
                });
                // 列表页
                $('.nav ul li').click(function () {
                    var index = $(this).index()-1;
                    box.animate({right:'0px'},400);
                    ele.find('.tit li').eq(index).addClass('hover').siblings().removeClass('hover');
                    $(ele.find('.cont').eq(index)).fadeIn(400).siblings('.cont').fadeOut(200);
                })
            }else{
                // 手机端
                /*$('.menu-side,.nav .menu,.detail .menu').click(function () {
                    box.animate({top:'0px'},400);
                })*/
                scope.showCont = function () {
                    box.animate({top:'0px'},400);
                }
                $('.menu-close').click(function () {
                    box.animate({top:'-100%'},400)
                })
                ele.find('.tit li').click(function () {
                    var index = $(this).index();
                    $(this).addClass('hover').siblings().removeClass('hover');
                    $(ele.find('.cont').eq(index)).show().siblings('.cont').hide();
                })
            }
        }
    }
}])